package com.fufufu.mail.product.service.impl;

import com.fufufu.mail.product.dao.BrandDao;
import com.fufufu.mail.product.dao.CategoryDao;
import com.fufufu.mail.product.service.BrandService;
import com.fufufu.mail.product.service.CategoryService;
import com.fufufu.mail.product.vo.CategoryBrandRalationResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fufufu.common.utils.PageUtils;
import com.fufufu.common.utils.Query;

import com.fufufu.mail.product.dao.CategoryBrandRelationDao;
import com.fufufu.mail.product.entity.CategoryBrandRelationEntity;
import com.fufufu.mail.product.service.CategoryBrandRelationService;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Resource
    private CategoryService categoryService;

    @Resource
    private BrandService brandService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        // 需要根据id查询品牌和分类的名字
        categoryBrandRelation.setCatelogName(categoryService.getById(categoryBrandRelation.getCatelogId()).getName());
        categoryBrandRelation.setBrandName(brandService.getById(categoryBrandRelation.getBrandId()).getName());
        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandName(name);
        categoryBrandRelationEntity.setBrandId(brandId);
        this.update(categoryBrandRelationEntity, new QueryWrapper<CategoryBrandRelationEntity>()
                .eq("brand_id", brandId));
    }

    /**
     *
     * 根据类别id查到对应的品牌id和品牌名称
     */
    @Override
    public List<CategoryBrandRelationEntity> getBrandByCatId(Long catId) {
        // 在service层最好是别用VO，返回entity就行了，然后再Controller进行VO的封装，这样的好处是如果这个service被其它方法调用的时候，
        // 因为返回的是整个entity的信息，所以就更加的泛用，比如Controller还一个方法是查brandId和catId，那么可以直接调用这个方法，然后
        // 在Controller层封装成需要的VO
        return baseMapper.selectList(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
    }

    @Override
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategory(catId, name);
    }

}